package com.team.arms.service_management_before.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.service_management_before.domain.BeforeMilitary;
import com.team.arms.service_management_before.domain.vo.BeforeMilitaryVo;
import com.team.arms.service_management_before.service.BeforeMilitaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yjh
 * @create 2022-06-23-20:41
 * @description:
 */
@Api( tags = "役前管理表")
@RestController
@RequestMapping("/serviceManagementBefore/BeforeMilitary")
public class BeforeMilitaryController extends BaseController {

    @Autowired
    private BeforeMilitaryService beforeMilitaryService;

    /**
     * 查询役前管理表
     * @return
     */
    @ApiOperation("查询役前管理表")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<BeforeMilitaryVo> list = beforeMilitaryService.selectList();
        PageInfo<BeforeMilitaryVo> pageInfo = new PageInfo<BeforeMilitaryVo>(list);
        PageVO<BeforeMilitaryVo> beforeMilitaryVoPageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, beforeMilitaryVoPageVO);
        beforeMilitaryVoPageVO.setResult(pageInfo.getList());
        beforeMilitaryVoPageVO.setPage(pageNum);
        return AjaxResult.success(beforeMilitaryVoPageVO);
    }

    /**
     * 根据id查询役前管理表
     * @param id
     * @return
     */
    @ApiOperation("根据id查询役前管理表")
    @GetMapping("/selectById")
    public AjaxResult selectById( @RequestParam(value = "before_military_id")String id){
        return AjaxResult.success(beforeMilitaryService.selectById(id));
    }

    /**
     *
     * @param field
     * @param content
     * @return
     */
    @ApiOperation("根据对应字段查询役前管理表")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@ApiParam("查询的字段")@RequestParam(value = "field") String field ,
                                    @ApiParam("查询的内容")@RequestParam(value = "content")String content){
        field = humpToUnderline(field);
        return AjaxResult.success(beforeMilitaryService.selectByFiled(field,content));
    }
    public static String humpToUnderline(String str) {
        String regex = "([A-Z])";
        Matcher matcher = Pattern.compile(regex).matcher(str);
        while (matcher.find()) {
            String target = matcher.group();
            str = str.replaceAll(target, "_"+target.toLowerCase());
        }
        return str;
    }

    /**
     * 新增役前管理表
     */
    @ApiOperation("新增役前管理表")
    @PostMapping("/install")
    public AjaxResult installBeforeMilitary(@RequestBody BeforeMilitary beforeMilitary){
        return toAjax(beforeMilitaryService.insertAll(beforeMilitary));
    }

    /**
     * 修改役前管理表
     * @param id
     * @param beforeMilitary
     * @return
     */
    @ApiOperation("修改役前管理表")
    @Log(title = "修改役前管理表", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateBeforeMilitary(
            @RequestParam(value = "before_military_id")String id,
            @RequestBody BeforeMilitary beforeMilitary){
        QueryWrapper<BeforeMilitary> wrapper = new QueryWrapper();
        wrapper.eq("before_military_id",id);
        BeforeMilitary byId = beforeMilitaryService.getById(id);
        return toAjax(beforeMilitaryService.update(beforeMilitary,wrapper));
    }

    /**
     * 删除役前管理表
     * @param id
     * @return
     */
    @ApiOperation("批量删除役前管理表")
    @DeleteMapping("/delete")
    public AjaxResult deleteBeforeMilitary(@RequestParam("class_id") List<String> id){
        return toAjax(beforeMilitaryService.deleteByListInformationIds(id));
    }

    @ApiOperation("删除役前管理表")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(@RequestParam("class_id") String id){
        return toAjax(beforeMilitaryService.deleteByListInformationId(id));
    }
}
